package com.enba.rbacadmin.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.enba.boot.core.utils.TreeNode;
import com.enba.boot.core.utils.TreeUtils;
import com.enba.rbacadmin.constant.CommonConstant;
import com.enba.rbacadmin.enums.MenuTypeEnum;
import com.enba.rbacadmin.module.auth.controller.resp.MenuTreeNodeResp;
import com.enba.rbacadmin.module.auth.entity.Menu;
import com.enba.rbacadmin.module.auth.entity.RoleMenu;
import com.enba.rbacadmin.module.auth.entity.UserRole;
import com.enba.rbacadmin.module.auth.mapper.MenuMapper;
import com.enba.rbacadmin.module.auth.mapper.RoleMapper;
import com.enba.rbacadmin.module.auth.mapper.RoleMenuMapper;
import com.enba.rbacadmin.module.auth.mapper.UserRoleMapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserMenuManager {

  @Autowired private UserRoleMapper userRoleMapper;
  @Autowired private RoleMenuMapper roleMenuMapper;
  @Autowired private MenuMapper menuMapper;
  @Autowired private RoleMapper roleMapper;

  /**
   * 获取用户目录和菜单
   *
   * @param userId
   * @return
   */
  public List<TreeNode> getUserMenu(Long userId) {
    // 获取用户角色
    List<UserRole> userRoleList =
        userRoleMapper.selectList(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, userId));
    Set<Long> roleSet = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toSet());

    // 获取角色权限
    List<RoleMenu> roleMenuList =
        roleMenuMapper.selectList(
            Wrappers.<RoleMenu>lambdaQuery().in(RoleMenu::getRoleId, roleSet));
    Set<Long> menuIdSet =
        roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toSet());

    // 获取用户菜单列表(目录，菜单)
    List<Menu> menuList =
        menuMapper.selectList(
            Wrappers.<Menu>lambdaQuery()
                .in(Menu::getId, menuIdSet)
                .ne(Menu::getType, MenuTypeEnum.BUTTON.getCode())
                .eq(Menu::getStatus, CommonConstant.yes)
                .eq(Menu::getDeleted, CommonConstant.no));

    // 转成 菜单树节点
    List<MenuTreeNodeResp> menuTreeNodeBoList = getMenuTreeNodeBos(menuList);

    // 返回树节点
    return TreeUtils.buildTree(menuTreeNodeBoList);
  }

  /**
   * 获取所有菜单
   *
   * @return
   */
  public List<TreeNode> getAllMenu() {
    List<Menu> menuList =
        menuMapper.selectList(
            Wrappers.<Menu>lambdaQuery()
                .eq(Menu::getStatus, CommonConstant.yes)
                .eq(Menu::getDeleted, CommonConstant.no));

    // 转成 菜单树节点
    List<MenuTreeNodeResp> menuTreeNodeBoList = getMenuTreeNodeBos(menuList);

    // 返回树节点
    return TreeUtils.buildTree(menuTreeNodeBoList);
  }

  private List<MenuTreeNodeResp> getMenuTreeNodeBos(List<Menu> menuList) {
    return menuList.stream()
        .map(
            e -> {
              MenuTreeNodeResp menuTreeNodeResp =
                  new MenuTreeNodeResp(e.getId(), e.getParentId(), e.getMenuName());
              menuTreeNodeResp.setHref(e.getUrl());
              menuTreeNodeResp.setIcon(e.getIcon());
              menuTreeNodeResp.setType(e.getType());
              menuTreeNodeResp.setStatus(e.getStatus());
              menuTreeNodeResp.setOpenType("_component");
              return menuTreeNodeResp;
            })
        .collect(Collectors.toList());
  }

  // 获取用户角色标识
  public Set<String> getUserRoleKey(Long userId) {
    return roleMapper.getUserRoleKey(userId);
  }

  // 获取用户权限码值
  public Set<String> getUserPermsKey(Long userId) {
    return menuMapper.getUserPermsKey(userId);
  }

  // 获取用户所有菜单权限id
  public Set<Long> getUserAllMenuIdSet(Long userId) {
    return roleMapper.getUserAllMenuSet(userId);
  }

  // 获取角色关联所有菜单权限id
  public Set<Long> getRoleAllMenuIdSet(Long roleId) {
    return roleMapper.getRoleAllMenuSet(roleId);
  }
}
